<?php


namespace api\controllers;

use common\components\Mqtt;
use common\core\actions\GetAction;
use common\core\actions\PostAction;
use common\helpers\BaseHelper;
use common\helpers\PHPExcel;
use server\EquipmentInterface;
use yii\base\Module;
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;


class EquipmentController extends Controller
{
    protected $equipment;

    public function __construct($id, Module $module, EquipmentInterface $equipment, array $config = [])
    {
        $this->equipment = $equipment;
        parent::__construct($id, $module, $config);
    }


    public function actions()
    {
        return [
            'line' => [
                'class' => GetAction::class,
                'data' => function () {
                    return $this->equipment->lineList();
                },
            ],
            'line_add' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'id',
                    'name',
                    'status'
                ],
                'data' => function ($data) {
                    return $this->equipment->updateLine($data);
                },
            ],
            'station' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'line_id',
                ],
                'data' => function ($data) {
                    return $this->equipment->stationList($data);
                },
            ],
            'station_add' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'id',
                    'name',
                    'status',
                    'line_id'
                ],
                'data' => function ($data) {
                    return $this->equipment->updateStation($data);

                },
            ],

            'lane' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'station_id',
                ],
                'data' => function ($data) {
                    return $this->equipment->laneList($data);

                },
            ],
            'lane_add' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'id',
                    'name',
                    'status',
                    'station_id'
                ],
                'data' => function ($data) {
                    return $this->equipment->updateLane($data);

                },
            ],

            'computer' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'type',//1:线路 2站点
                    'id',//1：线路id 2站点id
                ],
                'data' => function ($data) {
                    return $this->equipment->computerList($data);
                },
            ],
            'computer_add' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'name',
                    'ip',
                    'control_ip',
                    'line_id',
                    'station_id',
                    'lane_id',
                    'remote_username',
                    'remote_password',
                    'protocol'
                ],
                'data' => function ($data) {
                    $data['protocol'] = $data['protocol'] ?? 'vnc';
                    if ($data['protocol'] == 'vnc') {
                        $data['port'] = "5900";
                    } else if ($data['protocol'] == 'rdp') {
                        $data['port'] = "3389";
                    } else {
                        $this->invalidRequestException('链接方式只能是vnc或rpd');
                    }
                    return $this->equipment->addComputer($data);

                },
            ],
            'computer_edit' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    '_id',
                    'name',
                    'ip',
                    'control_ip',
                    'line_id',
                    'station_id',
                    'lane_id',
                    'remote_username',
                    'remote_password',
                    'status',
                    'protocol'
                ],
                'data' => function ($data) {
                    if ($data['protocol'] == 'vnc') {
                        $data['port'] = "5900";
                    } else if ($data['protocol'] == 'rdp') {
                        $data['port'] = "3389";
                    }
                    return $this->equipment->updateComputer($data);

                },
            ],
            'devices' => [
                'class' => GetAction::class,
                'validateFiled' => [
                ],
                'data' => function () {
                    return $this->equipment->devices();

                },
            ],
            'computer_delete' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'id'
                ],
                'data' => function ($data) {
                    return $this->equipment->deleteComputer($data);

                },
            ],
            'all_info' => [
                'class' => GetAction::class,
                'validateFiled' => [
                ],
                'data' => function () {
                    return $this->equipment->getAllInfo();

                },
            ],
            'item_info' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'ip',
                ],
                'data' => function ($data) {
                    return $this->equipment->getItemInfoByIP($data);
                },
            ],
            'devices_process' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'ip',
                    'process',
                ],
                'data' => function ($data) {
                    return $this->equipment->updateDevicesProcess($data);

                },
            ],
            'process_info' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'ip',
                ],
                'data' => function ($data) {
                    return $this->equipment->getProcessByIP($data);
                },
            ],

            'control_type' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'ip',
                    'type',
                    'index',
                    'operation',
                ],
                'data' => function ($data) {
                    $ip = trim($data['ip']);
                    switch ($data['type']) {
                        case 0:
                            $operation = $data['operation'] ? 'on' : 'off';
                            $url = 'http://' . $ip . '/api/usb/' . $data['index'] . '/' . $operation;
                            break;
                        case 1:
                            $url = 'http://' . $ip . '/api/mb/power/soft';
                            break;
                        case 2:
                            $url = 'http://' . $ip . '/api/mb/power/hard';
                            break;
                        case 3:
                            $url = 'http://' . $ip . '/api/mb/reset';
                            break;
                        case 4:
                            $url = 'http://' . $ip . '/api/disk/backup';
                            break;
                        case 5:
                            $url = 'http://' . $ip . '/api/disk/restore';
                            break;

                    }
                    if (isset($url)) {
                        $result = BaseHelper::getUrl($url);
                        if (ArrayHelper::getValue($result, 'statusText') == 'success') {
                            return true;
                        } else {
                            return false;
                        }
                    }
                },
            ],


            'control' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'ip',
                    'command'
                ],
                'data' => function ($data) {
                    if (!in_array($data['command'], ['restart', 'shutdown'])) {
                        $this->invalidRequestException('无效的指令');
                    }
                    $topic = "control/" . $data['ip'];
                    $message = $data['command'];
                    $client_id = 'yinlian:web';
                    $mqtt = new  Mqtt();
                    return $mqtt->sendMsg($topic, $message, $client_id);
                },
            ],

            'import_template' => [
                'class' => GetAction::class,
                'validateFiled' => [
                ],
                'data' => function () {
                    try {
                        PHPExcel::createTemplate();
                    } catch (\Exception $e) {
                        return $e->getMessage();
                    }
                    return 'template download success';

                },
            ],

            'import' => [
                'class' => PostAction::class,
                'validateFiled' => [

                ],
                'data' => function () {
                    $file = UploadedFile::getInstanceByName('file');
                    // 验证文件类型
                    if ($file) {
                        if (!in_array($file->type, PHPExcel::$excelMimeTypes)) {
                            $this->invalidParamException('上传文件类型不是Excel!');
                        }
                        try {
                            $objPHPExcel = PHPExcel::loadExamExcel($file->tempName);
                            //$sheetTitle = $objPHPExcel->getActiveSheet()->getTitle();
                            // 读取当前sheet数据.
                            $sheetData = PHPExcel::sheetToArray($objPHPExcel, 0);
                            // return $this->equipment->importExcelData($sheetData);

                        } catch (\Exception $e) {
                            $this->invalidParamException('上传出错啦～: ' . $e->getMessage());
                        }
                    } else {
                        $this->invalidParamException('没有上传文件');
                    }

                },

            ]

        ];
    }
}
